/*
 * Copyright 2004-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.card323.crpg.io.msn;

import com.card323.crpg.Engine;
import com.card323.crpg.PlayerManager;

import net.sf.jml.MsnContact;
import net.sf.jml.MsnMessenger;
import net.sf.jml.MsnSwitchboard;
import net.sf.jml.event.MsnMessageAdapter;
import net.sf.jml.message.MsnControlMessage;
import net.sf.jml.message.MsnDatacastMessage;
import net.sf.jml.message.MsnInstantMessage;

/**
 * @author Roger Chen
 */
public class EchoMessenger extends BasicMessenger {

	protected void initMessenger(MsnMessenger messenger) {

		messenger.addMessageListener(new MsnMessageAdapter() {
			public void instantMessageReceived(MsnSwitchboard switchboard,
					MsnInstantMessage message, MsnContact contact) {

				String email = contact.getEmail().getEmailAddress();
				MSNReceiverManager.getInstance().add_player(email, switchboard);
				if (!PlayerManager.getInstance().is_player_inited(email)) {
					Thread t = new Thread(new Engine(email, "msn"));
					t.start();
					MSNMessageSender sender=new MSNMessageSender(email);
					sender.send_message("游戏初始化。。");
					
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				// String email = contact.getEmail().getEmailAddress();
				MSNMessageReceiver receiver = MSNReceiverManager.getInstance()
						.get_message_receiver(email);

				if (receiver.is_waiting_message()) {
					receiver.set_message(message.getContent());
				}

			}

			public void controlMessageReceived(MsnSwitchboard switchboard,
					MsnControlMessage message, MsnContact contact) {
				// such as typing message and recording message
				switchboard.sendMessage(message);
			}

			public void datacastMessageReceived(MsnSwitchboard switchboard,
					MsnDatacastMessage message, MsnContact contact) {
				// such as Nudge
				switchboard.sendMessage(message);
			}

		});
	}

}